﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Caviar.SharedKernel.Entities.IM
{
    public class Message : SysUseEntity
    {
        /// <summary>
        /// 发送人ID
        /// </summary>
        public int FriendSenderId { get; set; }

        /// <summary>
        /// 接收人
        /// </summary>
        public Friend FriendReceiver { get; set; }

        /// <summary>
        /// 接收人Id
        /// </summary>
        public int FriendReceiverId { get; set; }

        /// <summary>
        /// 好友Token
        /// </summary>
        public string FriendToken { get; set; }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime SendTime { get; set; }

        /// <summary>
        /// 接收时间
        /// </summary>
        public DateTime? ReceiveTime { get; set; }

        /// <summary>
        /// 消息类型
        /// </summary>
        public MessageType Type { get; set; }

        /// <summary>
        /// 内容体
        /// </summary>
        public string ContentBody { get; set; }

        /// <summary>
        /// 是否已读
        /// </summary>
        public bool IsRead { get; set; } = false;

        /// <summary>
        /// 是最新消息
        /// </summary>
        public bool IsLatest { get; set; } = true;

        public void AddMessage(int friendSenderId,int receiverId,string friendToken, DateTime sendTime, MessageType type, string contentBody) 
        {
            FriendSenderId = friendSenderId;
            FriendReceiverId = receiverId;
            FriendToken = friendToken;
            SendTime = sendTime;
            Type = type;
            ContentBody = contentBody;
        }

        public void SetLatestMessage(List<Message> messages) 
        {
            foreach (var message in messages)
            {
                message.IsLatest = false;
            }
        }

        public Message SetIsRead() 
        {
            IsRead = true;
            return this;
        }
    }
}